package com.myblog.config.plus;


import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Property;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Configuration
@RequiredArgsConstructor
public class MybatisPlusGeneratorConfig {
    private final DynamicDataSourceProperties dynamicDataSourceProperties;


    /**
     * 生成代码
     *
     * @param tableNameList 表名集合
     * @param tablePrefix   表前缀,生成的类名会过滤掉
     */
    public void generateCode(List<String> tableNameList, String tablePrefix,String dataSourceName) {
        Map<String, DataSourceProperty> dataSourcePropertyMap = dynamicDataSourceProperties.getDatasource();
        DataSourceProperty dataSourceProperty = dataSourcePropertyMap.get(dataSourceName);
        String dir = System.getProperty("user.dir");
        FastAutoGenerator.create(dataSourceProperty.getUrl(), dataSourceProperty.getUsername(),
                dataSourceProperty.getPassword()).globalConfig(builder -> {
                    builder.author("") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir(String.format("%s\\src\\main\\java", dir)); // 指定输出目录
                }).packageConfig(builder -> {
                    builder.parent("com.myblog") // 设置父包名
                            //.moduleName(MODEL) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, String.format("%s\\src\\main" +
                                    "\\resources\\mapper", dir))) // 设置mapperXml生成路径
                            .entity("model.po").build();
                }).strategyConfig(builder -> {
                    builder.addInclude(tableNameList) // 设置需要生成的表名
                            .addTablePrefix(tablePrefix)// 设置过滤表前缀
                            .serviceBuilder().formatServiceFileName("%sService").entityBuilder().enableLombok().fileOverride()
                            .addTableFills(getFillList()).logicDeletePropertyName("ISDELETE").controllerBuilder();
                }).templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

    private List<IFill> getFillList() {
        List<IFill> fillList = new ArrayList<>(4);
        fillList.add(new Property("createrUserId", FieldFill.INSERT));
        fillList.add(new Property("createTime", FieldFill.INSERT));
        fillList.add(new Property("updateUserId", FieldFill.INSERT_UPDATE));
        fillList.add(new Property("updateTime", FieldFill.INSERT_UPDATE));
        return fillList;
    }

}
